﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zj.HttpCodeDemo.ConsoleView.Model;
using Zj.HttpCodeDemo.ConsoleView.Sample;
using Zj.HttpCodeDemo.ConsoleView.Sys;

namespace Zj.HttpCodeDemo.ConsoleView
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //var rt = TestAsync().Result;
                //foreach (var item in rt)
                //{
                //    Console.WriteLine("Text:{0},Url:{1}", item.Text, item.Url);
                //}

               
                {
                    FunctionalProgrammingSample.Sample();
                }
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static async Task<List<Category>> TestAsync()
        {
            string url = "https://www.cnblogs.com/";
            var rt = await HttpHelper.DownloadHtmlAsync(url);

            List<Category> categoryList = new List<Category>();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(rt);
            var categoryNodes = doc.DocumentNode.SelectNodes("//*[@id='cate_item']/li/a");
            if (categoryNodes != null && categoryNodes.Count > 0)
            {
                for (int i = 0; i < categoryNodes.Count - 2; i++)
                {
                    var category = new Category(categoryNodes[i].InnerText.Trim(), $"https://www.cnblogs.com{categoryNodes[i].Attributes["href"].Value}");
                    categoryList.Add(category);
                }
            }
            return categoryList;
        }
    }
}
